/**************************************************************************//**
*
* @copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date            Author       Notes
* 2020-12-12      Wayne        First version
*
******************************************************************************/

#include <rtconfig.h>
#include <rtdevice.h>
#include "modbus_interface.h"
#include "mqtt_protocol.h"
#if defined(RT_USING_PIN)
#include <drv_gpio.h>
#include <stdio.h>
/* defined the LED_R pin: PB13 */
#define LED_R   NU_GET_PININDEX(NU_PB, 13)

/* defined the LED_Y pin: PB8 */
#define LED_Y   NU_GET_PININDEX(NU_PB, 8)

#if !defined(BSP_USING_USBH)
/* defined the Key1 pin: PE10 */
#define KEY_1   NU_GET_PININDEX(NU_PE, 10)

/* defined the Key2 pin: PE12 */
#define KEY_2   NU_GET_PININDEX(NU_PE, 12)

static uint32_t u32Key1 = KEY_1;
static uint32_t u32Key2 = KEY_2;

void nu_button_cb(void *args)
{
    uint32_t u32Key = *((uint32_t *)(args));

    switch (u32Key)
    {
    case KEY_1:
        rt_pin_write(LED_Y, PIN_HIGH);
        break;
    case KEY_2:
        rt_pin_write(LED_Y, PIN_LOW);
        break;
    }
}
#endif

#endif
int net_set_default(char *devName)
{
    struct netdev *netdev = RT_NULL;

    netdev = netdev_get_by_name(devName);
    if (netdev == RT_NULL)
    {
        rt_kprintf("not find network interface device name(%s).\n", devName);
        return -1;
    }

    netdev_set_default(netdev);
    rt_kprintf("set default network interface device(%s) success.\n", devName);
    return 0;
}

int main(int argc, char **argv)
{
#if defined(RT_USING_PIN)
    int counter = 1000;

    /* set LED_R pin mode to output */
    rt_pin_mode(LED_R, PIN_MODE_OUTPUT);

    /* set LED_Y pin mode to output */
    rt_pin_mode(LED_Y, PIN_MODE_OUTPUT);

#if !defined(BSP_USING_USBH)
    /* set KEY_1 pin mode to input */
    rt_pin_mode(KEY_1, PIN_MODE_INPUT_PULLUP);

    /* set KEY_2 pin mode to output */
    rt_pin_mode(KEY_2, PIN_MODE_INPUT_PULLUP);

    rt_pin_attach_irq(KEY_1, PIN_IRQ_MODE_FALLING, nu_button_cb, &u32Key1);
    rt_pin_irq_enable(KEY_1, PIN_IRQ_ENABLE);

    rt_pin_attach_irq(KEY_2, PIN_IRQ_MODE_FALLING, nu_button_cb, &u32Key2);
    rt_pin_irq_enable(KEY_2, PIN_IRQ_ENABLE);
#endif
    int a = 9;
    printf("%b\n",a);

    initModbusRTU();
    initMqttDefaultParam();
    mqtt_start();
    net_set_default("esp0");
    while (1)
    {
        rt_pin_write(LED_R, PIN_HIGH);
        rt_thread_mdelay(500);
        rt_pin_write(LED_R, PIN_LOW);
        rt_thread_mdelay(500);
    }

#endif

    return 0;
}
#ifdef FINSH_USING_MSH
int defaultNet(int argc, char **argv)
{
    if (argc == 1)
    {
        rt_kprintf("bad parameter! e.g: defaultNet e0 or esp0 or ec200x\n");
    }
    else if (argc == 2)
    {
        rt_kprintf("network name : %s\n", argv[1]);
        if(net_set_default(argv[1]) == 0){
            mqtt_stop();
            sleep(5);
            mqtt_start();
        }
    }
    else
    {
        rt_kprintf("bad parameter! e.g: defaultNet e0 or esp0 or ec200x\n");
    }

    return 0;
}
MSH_CMD_EXPORT(defaultNet, configure default network);
#endif
